/*
 * AddressFrame.java
 * 
 * Copyright 2006 Sun Microsystems, Inc. ALL RIGHTS RESERVED Use of 
 * this software is authorized pursuant to the terms of the license 
 * found at http://developers.sun.com/berkeley_license.html .
 *
 */

package org.knoppers.examples;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * 
 */
public class AddressFrame extends javax.swing.JFrame implements ActionListener,
		ListSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5489417952657079017L;

	/** Creates new form AddressFrame */
	public AddressFrame() {
		initComponents();
		loadFrameIcon();
		windowAdapter = new WindowCloser();
		this.addWindowListener(windowAdapter);
		db = new AddressDao();
		db.connect();
		addressActionPanel.addActionListener(this);
		addressPanel.setEditable(false);
		List<ListEntry> entries = db.getListEntries();
		addressListPanel.addListEntries(entries);
		addressListPanel.addListSelectionListener(this);
	}

	/**
	 * Load our own "address book" icon into our frame window.
	 */
	private void loadFrameIcon() {
		// URL imgUrl =
		// AddressFrame.class.getResource("../../addressbook32.gif");
		// ImageIcon imgIcon = new ImageIcon(imgUrl);
		// Image img = imgIcon.getImage();
		this.setIconImage(null);

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		addressActionPanel = new AddressActionPanel();
		addressPanel = new AddressPanel();
		addressListPanel = new AddressListPanel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("Address Book Demo");
		getContentPane().add(addressActionPanel, java.awt.BorderLayout.SOUTH);

		getContentPane().add(addressPanel, java.awt.BorderLayout.CENTER);

		getContentPane().add(addressListPanel, java.awt.BorderLayout.WEST);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void cancelAddress() {
		addressPanel.clear();
		addressPanel.setEditable(false);
		ListEntry entry = addressListPanel.getSelectedListEntry();
		if (entry != null) {
			int id = entry.getId();
			Address address = db.getAddress(id);
			addressPanel.setAddress(address);
		}

	}

	private void newAddress() {
		addressPanel.clear();
		addressPanel.setEditable(true);

	}

	private void deleteAddress() {
		int id = addressPanel.getId();
		if (id != -1) {
			db.deleteRecord(id);
			int selectedIndex = addressListPanel.deleteSelectedEntry();
			if (selectedIndex >= 0) {
				selectedIndex = addressListPanel
						.setSelectedIndex(selectedIndex);
				ListEntry entry = addressListPanel.getSelectedListEntry();
				if (entry != null) {
					id = entry.getId();
					Address address = db.getAddress(id);
					addressPanel.setAddress(address);
				} else {
					addressPanel.clear();
				}
			}
		} else {
			addressPanel.clear();
		}
		addressPanel.setEditable(false);

	}

	private void editAddress() {
		int selected = addressListPanel.getSelectedIndex();
		if (selected >= 0) {
			addressPanel.setEditable(true);
		}

	}

	private void saveAddress() {
		if (addressPanel.isEditable()) {
			Address address = addressPanel.getAddress();
			int id = address.getId();
			String lname = address.getLastName();
			String fname = address.getFirstName();
			String mname = address.getMiddleName();
			if (id == -1) {
				id = db.saveRecord(address);
				address.setId(id);

				ListEntry entry = new ListEntry(lname, fname, mname, id);
				addressListPanel.addListEntry(entry);

			} else {
				db.editRecord(address);
				ListEntry entry = addressListPanel.getSelectedListEntry();
				entry.setFirstName(fname);
				entry.setMiddleName(mname);
				entry.setLastName(lname);
				addressListPanel.repaint();
			}
			addressPanel.setEditable(false);
		}
	}

	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		System.out.println("ActionEvent: " + actionCommand);
		if (actionCommand.equalsIgnoreCase("CANCEL_ADDRESS")) {
			cancelAddress();
		} else if (actionCommand.equalsIgnoreCase("NEW_ADDRESS")) {
			newAddress();
		} else if (actionCommand.equalsIgnoreCase("DELETE_ADDRESS")) {
			deleteAddress();
		} else if (actionCommand.equalsIgnoreCase("EDIT_ADDRESS")) {
			editAddress();
		} else if (actionCommand.equalsIgnoreCase("SAVE_ADDRESS")) {
			saveAddress();
		}
	}

	public static void main(String[] args) {
		AddressFrame app = new AddressFrame();
		app.setVisible(true);
	}

	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting()) {
			return;
		}
		JList<?> entryList = (JList<?>) e.getSource();
		ListEntry entry = (ListEntry) entryList.getSelectedValue();
		if (entry != null) {
			int id = entry.getId();
			Address address = db.getAddress(id);
			addressPanel.setAddress(address);
		} else {
			addressPanel.clear();
		}
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private AddressActionPanel addressActionPanel;
	private AddressListPanel addressListPanel;
	private AddressPanel addressPanel;
	// End of variables declaration//GEN-END:variables

	private AddressDao db;
	private WindowAdapter windowAdapter;

	class WindowCloser extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			db.disconnect();
		}

	}
}
